package dao;

import bean.Book;
import bean.BookCategory;
import db.MyHibernateSessionFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.List;

/**
 * Created by WeaponLin on 2015/11/19.
 */
public class BookCategoryDAO {

    public List<BookCategory> queryCategoryBySQL(String sql) {
        Transaction transaction = null;
        List<BookCategory> list = null;

        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();// 事务开启
            Query query = session.createSQLQuery("SELECT * from BookCategory where " + sql).addEntity(BookCategory.class);;
            list = query.list();
            transaction.commit();
            if(list.size() > 0){
                return list;
            }
            else{
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return null;
        }
    }

    public boolean addCategory(BookCategory bookCategory) {
        Transaction transaction = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();// 事务开启
            session.save(bookCategory);
            transaction.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return false;
        }
    }

    public boolean updateCategory(BookCategory bookCategory) {
        Transaction transaction = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();// 事务开启
            session.update(bookCategory);
            transaction.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return false;
        }
    }

    public boolean deleteCategory(String categotyid) {
        Transaction transaction = null;
        try {
            Session session = MyHibernateSessionFactory.getSessionFactory()
                    .getCurrentSession();
            transaction = session.beginTransaction();
            Query query = session.createQuery("from BookCategory where categoryid='"
                    + categotyid + "'");
            List<BookCategory> list = query.list();
            if (list.size() > 0) {
                session.delete(list.get(0));
                transaction.commit();
                return true;
            }
            else{
                transaction.commit();
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            transaction.commit();
            return false;
        }
    }

}
